import { exportFile } from "@/api/base/base.js";

const urlCodeConfig = {
  "/production/finishReceipt": "completionWh", // 成品入库单

  "/logistics/produReturnOrder": "returnMaterial", // 生产退料单

  "/basic/custom": "customerInfo", // 客户

  "/basic/supplier": "supplierInfo", // 供应商

  "/basic/unitOfMeasurement": "unitInfo", // 计量单位

  "/basic/materials": "materialInfo", // 物料

  "/basic/lineMaintenance": "lineInfo", // 线体维护

  "/basic/workStation": "stationInfo", // 工站维护

  "/basic/processMaintenance": "processInfo", // 工序维护

  "/basic/processRoute?1": "routeInfo", // 工艺路线 - 工艺路线

  "/basic/processRoute?2": "routeInProduct", // 工艺路线 - 关联产品

  "/basic/warehouseMaintenance": "warehouseInfo", // 仓库维护

  "/basic/warehouseBusinessType": "warehouseBizType", // 仓库业务类型

  "/basic/warehouseAuth": "userWarehousePerm", // 用户仓库权限

  "/basic/receptacle": "containerQuery", // 容器查询

  "/basic/containerType?container": "containerType", // 容器类型 - 容器类型

  "/basic/containerType?material": "containerTypeInMaterial", // 容器类型 - 容器类型与物料

  "/basic/materialInspectionStandard?material": "materialInspectionStandard", // 物料检验标准 - 物料检验标准

  "/basic/materialInspectionStandard?standard": "StandardInMaterial", // 物料检验标准 - 检验标准与物料

  "/basic/assetBrand": "assetBrand", // 资产品牌

  "/basic/assetModel": "assetModel", // 资产型号

  "/basic/deviceLedger": "equipmentLedger", // 设备台账

  "/production/workOrderManage": "workOrderManagement", // 工单管理

  "/logistics/purchaseOrder": "purchaseOrder", // 采购订单

  "/logistics/receivingManagement": "receiptInbound", // 收货计划

  "/logistics/prodMaterialRequisition": "productionMaterialOutbound", // 生产领料

  "/logistics/finishedShippingOrder": "finishedGoodsShipment", // 成品发货单

  "/logistics/saleOrder": "salesOrder", // 销售订单

  "/logistics/sundriesOutbound": "miscellaneousOutbound", // 杂项出库

  "/logistics/scrapStorage": "scrapInbound", // 报废入库单

  "/logistics/inventoryReview": "inboundAudit", // 入库审核

  "/logistics/outboundAudit": "outboundAudit", // 出库审核

  "/report/inventoryOnHand": "currentInventory", // 库存现有量

  "/logistics/transferOrder": "inventoryTransfer", // 调拨单

  "/report/processWip": "wipStatement", // wip报表

  "/report/incoming": "iqcBillStatement", // 来料报检

  "/report/transitRecord": "processCollectReport", // 过站记录

  "/report/putMaterialReport": "workOrderPutMaterialReport", // 工单投料报表

  "/accoutrement/spotProject": "checkItem", // 设备点检项

  "/accoutrement/spotCheckPlan": "checkPlan", // 设备点检计划

  "/accoutrement/spotQuery": "checkBill",

  "/basic/labelTemplate?1": "labelTemp", // 标签模板 - 标签模板

  "/basic/labelTemplate?2": "labelTempInMaterial", // 标签模板 - 标签模板与物料
};

/**
 * 导出处理
 * @param {Object} vm - 页面实例 this
 * @param {Object} params - 请求参数
 * @param {String} code - 同页面存在多个导出的标识
 */
export const exportHandle = async ({ vm, params = {}, code }) => {
  if (!vm) throw new Error("vm实例对象不能为空");

  await vm.$textShow({
    title: vm.$t("msg.exportTip"),
    content: [vm.$t("tip.export")],
  });

  let path = vm.$route.path;
  if (code) path = `${path}?${code}`;

  const exportCode = urlCodeConfig[path];
  if (!exportCode)
    throw new Error(`配置中不存在【${path}】的key，到@/utils/export.js检查`);

  const { data: res } = await exportFile(exportCode, params);
  if (res.code != 200) return vm.$message.error(res.msg);

  // vm.$message.success(vm.$t("msg.exportSuccess"));
  vm.$textShow({
    title: vm.$t("tip.label"),
    content: [vm.$t("msg.exportSuccess")],
  })
    .then(() => {
      vm.$router.push({ path: "/system/export" });
    })
    .catch(() => {});
};
